package com.songlanyun.modules.rabbitmq.entity;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.drive.ValueObject;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.rabbitmq.util.JodaTimeUtil;
import com.songlanyun.modules.rabbitmq.util.JsonUtil;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;


@Data
@NoArgsConstructor
@Accessors(chain = true)
@TableName("msg_log")
public class MsgLog implements ValueObject {

    /**
     * 消息唯一标识
     */
    @TableId(type = IdType.INPUT)
    private String msgId;
    /**
     * 消息体, json格式化
     */
    private String msg;
    /**
     * 交换机
     */
    private String exchange;
    /**
     * 路由键
     */
    private String routingKey;
    /**
     * 状态: 0投递中 1投递成功 2投递失败 3已消费
     */
    private Integer status;
    /**
     * 重试次数
     */
    private Integer tryCount;
    /**
     * 下一次重试时间
     */
    private Date nextTryTime;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 更新时间
     */
    private Date updateTime;

    public MsgLog(String msgId, Object msg, String exchange, String routingKey) {
        this.msgId = msgId;
        this.msg = JsonUtil.objToStr(msg);
        this.exchange = exchange;
        this.routingKey = routingKey;

        this.status = MqConstant.MsgLogStatus.DELIVERING;
        this.tryCount = 0;

        Date date = new Date();
        this.createTime = date;
        this.updateTime = date;
        this.nextTryTime = (JodaTimeUtil.plusMinutes(date, 1));
    }
}
